const app = getApp()

Page({
    data: {
        markers: [],
        latitude: 0,
        longitude: 0
    },

    onShow() {
        const db = app.globalData.db
        db.collection('minipet').get().then(res => {
            // res.data 包含该记录的数据
            const markers = res.data.map(d => {
                const { id, latitude, longitude, address, type } = d
                return {
                    id,
                    latitude,
                    longitude,
                    iconPath: type === 'buy' ? '/resources/buy.png' : '/resources/sell.png',
                    width: 40,
                    height: 40,
                    callout: {
                        content: address,
                        display: 'ALWAYS'
                    }
                }
            })
            this.setData({
                markers
            })
        })
    },

    onReady() {
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
                this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
            }
        })
        this.mapContext = wx.createMapContext('map')  
    },

    handleCenterClick() {
        this.mapContext.moveToLocation()
    },

    handleMarkerTap(e) {
        const markerId = e.detail.markerId
        wx.navigateTo({
          url: '/pages/details/details?id=' + markerId,
        })
    }
})